﻿/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace TeachMe.Application.Controls
{
    public class ImageGroupBox : Panel
    {
        private const int LineLeft = 4;
        private const int LineTop = 10;
        private const int ImageLeft = 4;
        private const int ImageTop = 18;
        private const int TextLeft = 10;

        public ImageGroupBox()
        {
            SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
        }

        [Bindable(true)]
        public Image Image { get; set; }

        [Localizable(true)]
        public string Caption { get; set; }

        protected override void OnPaint(PaintEventArgs e)
        {
            SizeF textSize = e.Graphics.MeasureString(Caption, Font);
            e.Graphics.DrawString(Caption, Font, SystemBrushes.ControlText, TextLeft, LineTop - ((int)textSize.Height / 2));

            DrawTopLine(e.Graphics, TextLeft + (int)textSize.Width, Width - (LineLeft * 2));

            if (Image != null)
            {
                e.Graphics.DrawImage(Image, new Rectangle(ImageLeft, ImageTop, Image.Width, Image.Height));
            }
            
            base.OnPaint(e);
        }

        private static void DrawTopLine(Graphics graphics, int left, int width)
        {
            graphics.DrawLine(Pens.Gray, LineLeft + left, LineTop, LineLeft + left + width, LineTop);
            graphics.DrawLine(Pens.White, LineLeft + left, LineTop + 1, LineLeft + left + width, LineTop + 1);
        }
    }
}
